package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.gson.Gson;
import com.hotel.BL.ReasonDetailLogic;

import data.ReasonDetail;

@SuppressWarnings("serial")
public class ReasonDetailService extends HttpServlet {

	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.ENGLISH);
	// localhost:8080/reasonDetailService?func=addReasonDetail&resId=&reason=&detail=&cost=
		// localhost:8080/ReasonDetailService?func=deleteReasonDetail&resId=
		// localhost:8080/ReasonDetailService?func=loadReasonDetail
		public void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws IOException {

			String func = req.getParameter("func");

			try {
				if (func.equals("deleteReasonDetail")) {
					DeleteReasonDetail( req, resp);
				}
				if (func.equals("loadReasonDetail")) {
					LoadReasonDetail(req, resp);
				}
				if (func.equals("editReasonDetail")) {
					EditReasonDetail(req, resp);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	private void EditReasonDetail( HttpServletRequest req,
			HttpServletResponse resp) throws EntityNotFoundException, IOException, ParseException {
		Gson gson = new Gson();
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long reasonDetailId = Long.parseLong(req.getParameter("reasonDetailId"));
		String arrayResId =req.getParameter("arrayResId");
		ReasonDetail rd = new ReasonDetailLogic().EditReservation(hotelId,reasonDetailId,arrayResId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rd));
	}
	private void DeleteReasonDetail( HttpServletRequest req,
				HttpServletResponse resp) throws NumberFormatException, EntityNotFoundException, IOException {
			// TODO Auto-generated method stub
		String resId = req.getParameter("resId");
		ReasonDetail rd = new ReasonDetailLogic().DeleteReasonDetail(Long.parseLong(resId));
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rd));
			
		}

	private void LoadReasonDetail(HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<ReasonDetail> rd = new ReasonDetailLogic().GetAllReasonDetail(hotelId);
		
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rd));
	}

}
